package com.globaleducation365.cartoonv1.view;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.bzydroid.network.http.AbsRequestHandler;
import com.bzydroid.network.http.NetworkResponse;
import com.bzydroid.network.http.client.BzydroidHttpClient;
import com.bzydroid.network.http.exception.BzydroidNetworkExeception;
import com.bzydroid.network.http.response.HttpJSONObjectResponse;
import com.bzydroid.utils.BzydroidLog;
import com.globaleducation365.cartoon.R;
import com.globaleducation365.cartoonv1.E4KidsConfig;
import com.globaleducation365.cartoonv1.E4KidsSlidingMenuActivity;
import com.globaleducation365.cartoonv1.adapter.AlbumIndexFragmentAdapter;
import com.globaleducation365.cartoonv1.adapter.SlideShowImageAdapter;
import com.globaleducation365.cartoonv1.adapter.VideoIndexFragmentAdapter;
import com.globaleducation365.cartoonv1.adapter.AlbumIndexFragmentAdapter.IClickPla;
import com.globaleducation365.cartoonv1.adapter.SlideShowImageAdapter.setOnClickItemListener;
import com.globaleducation365.cartoonv1.adapter.VideoIndexFragmentAdapter.ClickItemVideo;
import com.globaleducation365.cartoonv1.customize.CustomGridView;
import com.globaleducation365.cartoonv1.customize.NotInterceptInPager;
import com.globaleducation365.cartoonv1.entity.InfoAds;
import com.globaleducation365.cartoonv1.entity.InfoAlbum;
import com.globaleducation365.cartoonv1.entity.InfoVideo;
import com.globaleducation365.cartoonv1.service.RequestService;
import com.globaleducation365.cartoonv1.streaming.MainActivity;
import com.globaleducation365.cartoonv1.util.Debug;
import com.globaleducation365.cartoonv1.util.Util;
import com.ironsource.mobilcore.CallbackResponse;
import com.ironsource.mobilcore.MobileCore;

public class IndexNoTabActivity extends E4KidsSlidingMenuActivity implements OnClickListener {

    private ImageView ivABCancelSearch;
    private ImageView ivABSearch;
    private EditText edtABSearch;
    private LinearLayout llContainEdt;
    private ImageView tvABTitle;
    private ImageView ivABLogo;
    
    private VideoIndexFragmentAdapter adapterVideo;
	private AlbumIndexFragmentAdapter adapterAlbum;
	private SlideShowImageAdapter adapterAds;
	
//	private ArrayList<InfoVideo> dataAds;
	private ArrayList<InfoVideo> dataVideo;
	private ArrayList<InfoAlbum> dataPlaylist;
	
	private NotInterceptInPager viewPager;
	private CustomGridView gvVideo;
	private RelativeLayout rlContainPager;
	
	private TextView tvTitleVideo;
	
	private LinearLayout llLoading;
	private LinearLayout llNotInternet;
	
	private ScrollView scroller;
	
	private View root;
	
	private Handler slideHandler;
	
	private boolean isTouch = false;
	private int pageViewPager;
	private LinearLayout llListCat;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.layout_v2_index);

	this.setBehindContentView(R.layout.frame_layout_container);
	// this.getSlidingMenu().setSecondaryMenu(R.layout.frame_right_container);
	this.setDefaultBar();
	configLeftSlideMenu();
	// this.removeSlidingMenu();

	init();

    }

    private void init() {

	
	ivABCancelSearch = actionBar.getIvABCancelSearch();
	ivABSearch = actionBar.getIvABSearch();
	edtABSearch = actionBar.getEdtABSearch();
	llContainEdt = actionBar.getLlContainEdt();
	ivABLogo = actionBar.getIvABLogo();
	tvABTitle = actionBar.getIvTitle();
	
	ivABLogo.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View arg0) {
		toggle();
	    }
	});

	// edtABSearch.setOnEditorActionListener(searchAction);
	ivABSearch.setOnClickListener(this);
	ivABCancelSearch.setOnClickListener(this);

	edtABSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
	edtABSearch.setOnEditorActionListener(new OnEditorActionListener() {

	    @Override
	    public boolean onEditorAction(TextView arg0, int actionID,
		    KeyEvent arg2) {
		// RequestUrlService.requestSearchNameSinger(delegateSinger,
		// "tuan hung");
		String text = edtABSearch.getText().toString().trim();
		if (!text.equals("") || !TextUtils.isEmpty(text)) {
		   
		    goActivitySearch(IndexNoTabActivity.this,SearchResultActivity.class, E4KidsConfig.KEY_INTENT_SEARCH, text);
		}
		return true;
	    }
	});
	

    	slideHandler = new Handler();
    
	viewPager = (NotInterceptInPager)findViewById(R.id.viewpager);
	
	DisplayMetrics metrics = getResources().getDisplayMetrics();
	int width = metrics.widthPixels;
	
	int widthImage = width -2*(getResources()
		.getDimensionPixelOffset(R.dimen.item_video_padding)) ;
	int heightImage = (width * 67) / 100;
	
	RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widthImage, heightImage);
	
	int marginPager = getResources()
		.getDimensionPixelOffset(R.dimen.item_video_padding);
	params.setMargins(marginPager, 0, marginPager, 0);
	viewPager.setLayoutParams(params);
	
	gvVideo = (CustomGridView)findViewById(R.id.gvVideo);
	tvTitleVideo = (TextView) findViewById(R.id.tvTitleVideo);
	
	llListCat = (LinearLayout) findViewById(R.id.llCate);
	
	scroller = (ScrollView) findViewById(R.id.index_layout_scroller);
	
	llLoading = (LinearLayout) findViewById(R.id.ll_loading);
	llNotInternet = (LinearLayout) findViewById(R.id.llNoInternet);
	
	rlContainPager = (RelativeLayout) findViewById(R.id.index_layout_viewpager);
	
//	dataAds = new ArrayList<InfoVideo>();
	dataPlaylist = new ArrayList<InfoAlbum>();
	dataVideo = new ArrayList<InfoVideo>();
	
	adapterAds = new SlideShowImageAdapter(IndexNoTabActivity.this , imageLoader , options);
	
	RequestService.rqHomeIndex(packageName,new RequestHome());
//	loadAccess();
	
	viewPager.setAdapter(adapterAds);
	
	adapterAds.setOnClickItemListener(new setOnClickItemListener() {
	    
	    @Override
	    public void onCallClick(boolean isPla, String vid,
		    String linkVideo) {
		Debug.debug("IsPlay : " + isPla + "\nID : " + vid);
		if (isPla) {
		    Bundle bundle = new Bundle();
		    bundle.putString(E4KidsConfig.KEY_INTENT_AID, vid);

		    gotoActivity(ListVideoByPlaActivity.class, bundle);
		} else {
		    Bundle bundle = new Bundle();
		    bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
		    bundle.putString(E4KidsConfig.KEY_INTENT_LINK, linkVideo);

		    gotoActivity(PlayVideo.class, bundle);
		}
	    }

	});
	pageViewPager = 0;
	updateUI();
	
//	viewPager.setOnPageChangeListener(this);
	
	viewPager.setOnTouchListener(onSlideTouch);

	
    }
    
    /** Event to handle slide-show when user on touch */
	private OnTouchListener onSlideTouch = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_MOVE:
				isTouch = true;
				// Remove runnable when moving
				slideHandler.removeCallbacks(mRunnable);
				break;
			case MotionEvent.ACTION_UP:
				isTouch = false;
				// Repost thread
				slideHandler.postDelayed(mRunnable, 2000);
				break;

			}
			return false;
		}
	};
	
	/** Runnable to auto sliding */
	private Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			if (!isTouch) {
				pageViewPager += 1;
				if (pageViewPager == adapterAds.getCount()) {
					pageViewPager = 0;
				}
				viewPager.setCurrentItem(pageViewPager, true);
			}
			slideHandler.postDelayed(mRunnable, 3000);
		}
	};
	
	private void updateUI() {
		new Handler().postDelayed(mRunnable, 3000);
	}
	
	private class RequestHome extends AbsRequestHandler
	{
	    private InfoAds infoAds;

	    @Override
	public void onWait(BzydroidHttpClient arg0) {
	    super.onWait(arg0);
	    llLoading.setVisibility(View.VISIBLE);

	    if (!Util.isConnectingToInternet(IndexNoTabActivity.this)) {
		scroller.setVisibility(View.GONE);
		llNotInternet.setVisibility(View.VISIBLE);
		if (llLoading.getVisibility() == View.VISIBLE) {
		    llLoading.setVisibility(View.GONE);
		}
	    } else {
		scroller.setVisibility(View.VISIBLE);
		llNotInternet.setVisibility(View.GONE);
	    }
	}
	    @Override
	    public void onStarted(BzydroidHttpClient arg0) {
	        super.onStarted(arg0);
	        BzydroidLog.debug("onStarted");
	    }
	    @Override
	    public void onSuccess(BzydroidHttpClient arg0, NetworkResponse arg1) {
		
		HttpJSONObjectResponse jsonObjectResponse = (HttpJSONObjectResponse)arg0.getResponse();
		
		try {
		    JSONObject object = jsonObjectResponse.toJSONObject();
		    
		    Debug.debug("Du lieu Response : " + object.toString());
		    
		    JSONObject jObjData = object.getJSONObject(E4KidsConfig.KEY_DATA);
		    
		    int code = jObjData.getInt(E4KidsConfig.KEY_CODE);
		    
		    if(code == 1)
		    {
			//Debug.debug("Vao day : " + jObj);
			
			LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			JSONArray jArrAds = jObjData.getJSONArray(E4KidsConfig.Home.KEY_ADS);
			
			JSONArray jArrVideo = jObjData.getJSONArray(E4KidsConfig.Home.KEY_VIDEO);
			
			JSONArray jArrCats = jObjData.getJSONArray(E4KidsConfig.Home.KEY_CAT);
			
			Debug.debug("jArrAds : " + jArrAds.toString());
			Debug.debug("jArrVideo : " + jArrVideo.toString());
			
			int lengthAds = jArrAds.length();
			int lengthVideo = jArrVideo.length();
			int lengthCat = jArrCats.length();
			
		    if (lengthAds <= 0) {
			rlContainPager.setVisibility(View.GONE);
		    } else {
			rlContainPager.setVisibility(View.VISIBLE);
			
			for (int i = 0; i < lengthAds; i++) {
				JSONObject objectAds = jArrAds.getJSONObject(i);
				
				String id = objectAds.getString("id");
				String title = objectAds.getString("title");
				String image = objectAds.getString("image");
				int type = objectAds.getInt("type");
				if(type == 0)
				{
				    infoAds = new InfoAds(id, title, image, type , "");
				}
				else if(type == 1)
				{
				    String idYoutube = objectAds.getString("id_youtube");
				    infoAds = new InfoAds(id, title, image, type , idYoutube);
				}
				adapterAds.add(infoAds);
			    }
		    }
		    
		    //Fill video
		    if (lengthVideo <= 0) {
			gvVideo.setVisibility(View.GONE);
			tvTitleVideo.setVisibility(View.GONE);
		    } else {
			gvVideo.setVisibility(View.VISIBLE);
			tvTitleVideo.setVisibility(View.VISIBLE);
			
			  for (int i = 0; i < lengthVideo; i++) {
				JSONObject objectVideo = jArrVideo.getJSONObject(i);
				
				InfoVideo infoVideo = parseJsonVideo(objectVideo);
				
				dataVideo.add(infoVideo);
			    }
			  adapterVideo = new VideoIndexFragmentAdapter(IndexNoTabActivity.this, dataVideo, imageLoader , options);
			  gvVideo.setAdapter(adapterVideo);
			  
			  adapterVideo.setIclickItemVideo(new ClickItemVideo() {

			    @Override
			    public void clickItem(String vid, String id) {
				Bundle bundle = new Bundle();
				bundle.putString(E4KidsConfig.KEY_INTENT_VID, vid);
				bundle.putString(E4KidsConfig.KEY_INTENT_LINK, id);
				
				gotoActivity(PlayVideo.class, bundle);
			    }
			});
			  
		    }
		    
		    //Fill List cat
		    if (lengthCat <= 0) {
			llListCat.setVisibility(View.GONE);
		    } else {
			llListCat.setVisibility(View.VISIBLE);
			for (int i = 0; i < lengthCat; i++) {
				JSONObject objectLstCat = jArrCats.getJSONObject(i);
				
				String catTitle = objectLstCat.getString("title");
				
				View vListCat = mInflater.inflate(R.layout.layout_item_cat_index, null);
				
				TextView tvTitle = (TextView)vListCat.findViewById(R.id.tvTitlePla);
				
				CustomGridView gvListCat = (CustomGridView)vListCat.findViewById(R.id.gvAlbum);
				
				tvTitle.setText(catTitle);
				
				if(!objectLstCat.isNull("playlists")){
					JSONArray jArrDetailCat = objectLstCat.getJSONArray("playlists");
					
					
					
					dataPlaylist = new ArrayList<InfoAlbum>();
					
					int lengthDetailCat = jArrDetailCat.length();
					
					for (int j = 0; j < lengthDetailCat; j++) {
//					    
//					    TextView tv = new TextView(getActivity());
//					    
//					    tv.setText(" ->" + j);
//					    View vDetailCat = mInflater.inflate(R.layout.layout_gv_lstcat, null);
					    
//					    CustomGridView gvCatDetail = (CustomGridView)vDetailCat.findViewById(R.id.gvAlbum);
					    
					    JSONObject jObjDetaiCat = jArrDetailCat.getJSONObject(j);
					    
					    Debug.debug("Chi tiet Album trong Cat : " + i + " : " + jObjDetaiCat.toString());
					    
					    InfoAlbum infoAlbum = parseJsonPla(jObjDetaiCat , "");
						
					    dataPlaylist.add(infoAlbum);
					}
					adapterAlbum = new AlbumIndexFragmentAdapter(IndexNoTabActivity.this, dataPlaylist, imageLoader , options);
					    
					gvListCat.setAdapter(adapterAlbum);
					
					adapterAlbum.setClickPla(new IClickPla() {
					    
					    @Override
					    public void clickItemPla(String id) {
					    	Bundle b = new Bundle();
						
						b.putString(E4KidsConfig.KEY_INTENT_AID, id);
						gotoActivity(ListVideoByPlaActivity.class, b);
						app.setCatID(Integer.valueOf(id));
					    }
					});
				}
				llListCat.addView(vListCat, i);
			    }
//			adapterAlbum.setClickPla(new IClickPla() {
//			    
//			    @Override
//			    public void clickItemPla(String id) {
//			    	Bundle b = new Bundle();
//				
//				b.putString(E4KidsConfig.KEY_INTENT_AID, id);
//				gotoActivity(ListVideoByPlaActivity.class, b);
//				application.setCatID(Integer.valueOf(id));
//
//				Intent intent = new Intent(getActivity(),
//						CatDetailActivity.class);
//				startActivity(intent);
//				application.setIndex(false);
//			    }
//			});
		    }
		    
		    scroller.setVisibility(View.VISIBLE);
		}
		} catch (JSONException e) {
		    e.printStackTrace();
		}
	    }
	    @Override
	    public void onError(BzydroidHttpClient arg0,
	            BzydroidNetworkExeception arg1) {
	        super.onError(arg0, arg1);
	        BzydroidLog.debug("onError");
	        
	        if (!Util.isConnectingToInternet(IndexNoTabActivity.this)) {
			scroller.setVisibility(View.GONE);
			llNotInternet.setVisibility(View.VISIBLE);
			if (llLoading.getVisibility() == View.VISIBLE) {
			    llLoading.setVisibility(View.GONE);
			}
		    } else {
			scroller.setVisibility(View.VISIBLE);
			llNotInternet.setVisibility(View.GONE);
		    }
	    }
	    
	    @Override
	    public void onFinished(BzydroidHttpClient arg0) {
	        super.onFinished(arg0);
	        BzydroidLog.debug("onFinished");
	        llLoading.setVisibility(View.GONE);
	    }
	    
	
	}

    private void showDialog() {

	if (dialog == null) {
	    dialog = new Dialog(IndexNoTabActivity.this);
	    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	dialog.setCancelable(true);
	dialog.setCanceledOnTouchOutside(false);
	dialog.setContentView(R.layout.layout_exit);

	Button btLeft = (Button) dialog.findViewById(R.id.btExitLeft);
	Button btRight = (Button) dialog.findViewById(R.id.btExitRight);
	Button btCenter = (Button) dialog.findViewById(R.id.btExitCenter);
	Button btIntentApp = (Button) dialog.findViewById(R.id.btIntentApp);
//	Button btDownload = (Button) dialog.findViewById(R.id.btDownload);
//	Button btSkip = (Button) dialog.findViewById(R.id.btSkip);
	final LinearLayout llLstAds = (LinearLayout) dialog
		.findViewById(R.id.llAds);
	JSONArray jArrLstAds = app.getjArrAdsExit();
	int lengthAds = jArrLstAds.length();
	for (int i = 0; i < lengthAds; i++) {
	    try {
	    JSONObject jObjDetailAds = jArrLstAds.getJSONObject(i);
	    
	    LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View vAds = mInflater.inflate(R.layout.layout_banner_my_ads, null);
	    
	    ImageView ivAvatar = (ImageView)vAds.findViewById(R.id.ivAds);
	    TextView tvNameAds = (TextView)vAds.findViewById(R.id.tvNameAds);
	    TextView tvContentAds = (TextView)vAds.findViewById(R.id.tvContentAds);
	    final LinearLayout llMyads = (LinearLayout)vAds.findViewById(R.id.llMyads);
	    
	    Button btDownloadAds = (Button)vAds.findViewById(R.id.btDownload);
	    Button btSkip = (Button)vAds.findViewById(R.id.btSkip);
	    
	    
	    String linkImage = jObjDetailAds.getString("image");
	    String name = jObjDetailAds.getString("title");
	    String content = jObjDetailAds.getString("content");
	    final String linkMarket = jObjDetailAds.getString("link");
	    
	    tvNameAds.setText(name);
	    tvContentAds.setText(content);
	    
	    imageLoader.displayImage(linkImage, ivAvatar, options);
	    
	    btDownloadAds.setOnClickListener(new OnClickListener() {
	        
	        @Override
	        public void onClick(View v) {

			String packageName = linkMarket;
			try {
			    StringBuilder strb = new StringBuilder();
			    strb.append("market://details?id=").append(
				    packageName);
			    Debug.debug("Market : " + strb.toString());

			    Intent intent = new Intent(Intent.ACTION_VIEW);
			    intent.setData(Uri.parse(strb.toString()));
			    startActivity(intent);
			} catch (ActivityNotFoundException activityNotFound) {
			    // to handle play store not installed scenario
			    Intent intent = new Intent(
				    Intent.ACTION_VIEW,
				    Uri.parse("http://play.google.com/store/apps/details?id="
					    + packageName));
			    startActivity(intent);
			}
		    
	        }
	    });
	    
	    btSkip.setOnClickListener(new OnClickListener() {
	        
	        @Override
	        public void onClick(View v) {
	    		llMyads.setVisibility(View.GONE);
	        }
	    });
	    
	    llLstAds.addView(vAds, i);
	    } catch (JSONException e) {
		e.printStackTrace();
	    }
	}

	dialog.setOnCancelListener(new OnCancelListener() {

	    @Override
	    public void onCancel(DialogInterface arg0) {
		MobileCore.showOfferWall(IndexNoTabActivity.this,
			new CallbackResponse() {
			    @Override
			    public void onConfirmation(TYPE arg0) {
				hidenDialog();
				// showDialog();
			    }
			});
		// hidenDialog();
	    }
	});

	btRight.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		
	    }
	});
	
	btIntentApp.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		try {
		    StringBuilder strb = new StringBuilder();
		    strb.append("market://search?q=pub:globaledu");
		    Debug.debug("Market : " + strb.toString());

		    Intent intent = new Intent(Intent.ACTION_VIEW);
		    intent.setData(Uri.parse(strb.toString()));
		    startActivity(intent);
		} catch (ActivityNotFoundException activityNotFound) {
		    // to handle play store not installed scenario
		    Intent intent = new Intent(
			    Intent.ACTION_VIEW,
			    Uri.parse("http://play.google.com/store/search?q=pub:globaledu"));
		    startActivity(intent);
		}

	    }
	});

	btLeft.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		
		hidenDialog();
//		onBackPressed();
		IndexNoTabActivity.this.finish();
	    }
	});

	btCenter.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		runOnUiThread(new Runnable() {

		    @Override
		    public void run() {
			String packageName = getApplicationContext()
				.getPackageName();
			try {
			    StringBuilder strb = new StringBuilder();
			    strb.append("market://details?id=").append(
				    packageName);
			    Debug.debug("Market : " + strb.toString());

			    Intent intent = new Intent(Intent.ACTION_VIEW);
			    intent.setData(Uri.parse(strb.toString()));
			    startActivity(intent);
			} catch (ActivityNotFoundException activityNotFound) {
			    // to handle play store not installed scenario
			    Intent intent = new Intent(
				    Intent.ACTION_VIEW,
				    Uri.parse("http://play.google.com/store/apps/details?id="
					    + packageName));
			    startActivity(intent);
			}
		    }
		});
	    }
	});

	dialog.show();
    }

    private void hidenDialog() {
	if (dialog != null) {
	    dialog.dismiss();
	    dialog = null;
	}
    }

    @Override
    public void onBackPressed() {
	quitApp();
    }

    @Override
    public void onClick(View arg0) {
	switch (arg0.getId()) {
	case R.id.iv_search:
	    if (llContainEdt.getVisibility() == View.GONE) {
		llContainEdt.setVisibility(View.VISIBLE);
		ivABSearch.setVisibility(View.GONE);
		tvABTitle.setVisibility(View.GONE);
		edtABSearch.setFocusable(true);
		edtABSearch.requestFocus();
		showBoard(edtABSearch);
	    }
	    break;
	case R.id.iv_cancel:
	    String text = edtABSearch.getText().toString().trim();
	    if (!text.equals("")) {
		edtABSearch.setText("");
	    } else {
		llContainEdt.setVisibility(View.GONE);
		ivABSearch.setVisibility(View.VISIBLE);
		tvABTitle.setVisibility(View.VISIBLE);
		edtABSearch.setFocusable(true);
		hiddenBoard(edtABSearch);
	    }
	    break;
	}
    }
    
    
    private class RequestAccess extends AbsRequestHandler {
  	@Override
  	public void onStarted(BzydroidHttpClient arg0) {
  	    super.onStarted(arg0);
  	    Debug.debug("onStarted");
  	}

  	@Override
  	public void onSuccess(BzydroidHttpClient arg0, NetworkResponse arg1) {
  	    HttpJSONObjectResponse jsonObjectResponse = (HttpJSONObjectResponse) arg0
		    .getResponse();
  	    
  	    Debug.debug("HttpJSONObjectResponse Access : " + jsonObjectResponse.toString());
  	    
  	    try {
		JSONObject jObjResponse = new JSONObject(jsonObjectResponse.toString());
		
		JSONObject jObjData = jObjResponse.getJSONObject("data");
		
		int codeAccess = jObjData.getInt("code");
		
		app.setAccess(codeAccess);
	    } catch (JSONException e) {
		e.printStackTrace();
	    }
  	}

  	@Override
  	public void onError(BzydroidHttpClient arg0,
  		BzydroidNetworkExeception arg1) {
  	    super.onError(arg0, arg1);
  	    Debug.debug("onError");
  	    
  	  RequestService.rqHomeIndex(packageName,new RequestHome());
  	}
  	@Override
  	public void onFinished(BzydroidHttpClient arg0) {
  	    super.onFinished(arg0);
  	    Debug.debug("onFinished");
  	  RequestService.rqHomeIndex(packageName,new RequestHome());
  	}
      }
    
//    private void loadAccess(){
//	RequestService.rqAccess(new RequestAccess());
//    }
}
